﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.ComponentModel;

namespace ReplayHouse
{
    class UrlListBusiness
    {
        WebRequestBusiness requestBusiness = new WebRequestBusiness();

        /// <summary>
        /// 根据配置得到主URL
        /// </summary>
        /// <param name='listbox'></param>
        /// <returns></returns>
        public List<string> GetMainUrlList(List<string> listbox)
        {
            List<string> urlList = new List<string>();
            for (int i = 0; i < listbox.Count; i++)
            {
                urlList.Add(listbox[i]);
            }
            return urlList;
        }

        /// <summary>
        /// 根据主URL得到子URL
        /// </summary>
        /// <param name='listbox'></param>
        /// <returns></returns>
        public List<string> GetChildUrlList(string mainUrl)
        {
            string statusCode = string.Empty;
            List<string> resultList = new List<string>();
            int count = 1;
            string tempUrl = mainUrl;
            bool searchResult = true;

            do
            {
                if (count != 1)
                {
                    mainUrl = tempUrl;
                    mainUrl += "?p=" + count;
                }
                string result = requestBusiness.GetHtmlResponse(mainUrl, ref statusCode);
                if (!result.Contains(ReplyHouseConst.FilterWord))
                {
                    string regexExpress = "(?<url>(?<=ref ?= ?('|\")?)[^'\">]+?(?='|\"|>))";
                    Regex regex = new Regex(regexExpress, RegexOptions.IgnoreCase);
                    var matchResult = regex.Matches(result);

                    foreach (Match item in matchResult)
                    {
                        var url = item.Groups["url"].Value;
                        if (url.Contains("publish/rent") && !url.Contains("add"))
                        {
                            resultList.Add(url);
                        }
                    }

                    count++;
                }
                else
                {
                    searchResult = false;
                }
            } while (searchResult);

            return resultList;
        }

        /// <summary>
        /// 回复数据
        /// </summary>
        /// <param name='childUrl'></param>
        public void PostMessage(List<string> listbox, string message, BackgroundWorker workder)
        {
            List<string> mainUrl = GetMainUrlList(listbox);
            foreach (var url in mainUrl)
            {
                List<string> childUrl = GetChildUrlList(url);
                foreach (var item in childUrl)
                {
                    if (!UrlPolicy(item))
                    {
                        workder.ReportProgress(1, item);
                        requestBusiness.SendMessage(message, item);
                    }
                }
            }
        }

        /// <summary>
        /// Url回复策略
        /// </summary>
        /// <param name='requestUrl'></param>
        /// <returns></returns>
        private bool UrlPolicy(string requestUrl)
        {
            string statusCode = string.Empty;
            string result = requestBusiness.GetHtmlResponse(requestUrl, ref statusCode);
            if (result.Contains(ReplyHouseConst.PolicyKey))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
